// pages/category/category.js
Page({
  // TDESIGN侧边栏组件
  offsetTopList: [],
  data: {
    sideBarIndex: 10016,
    categories: [],
    newCategories: [],
  },
  onLoad() {
    this.getCateList();
  },
  // 点击跳转搜索页
  toSearch() {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  onSideBarChange(e) {
    const { value } = e.detail;

    this.setData({ sideBarIndex: value });

    // 根据catid筛选右侧展示宫格数据，存到新数组
    const newCategories = this.data.categories.reduce((obj, item) => {
      if (item.category_id === value) {
        obj = item;
      }
      return obj;
    });
    if (newCategories.children) {
      this.setData({ newCategories: newCategories.children });
    } else {
      this.setData({ newCategories: [] });
    }
  },

  //   获取分类数据
  async getCateList() {
    const res = await wx.http.get("/category/list");
    const categories = res.data.data.list;
    this.setData({ categories });
    // 初始化右侧展示宫格数据，存到新数组
    this.setData({newCategories: categories[0].children})
  },

  // 跳转列表页
  toList(ev) {
    wx.navigateTo({
      url: `../goodslist/goodslist?goodsName=${ev.mark.goodsName}`
    })
  }
})